merged master
[lhc/web/wiklou.git] / includes / filerepo / FileRepo.php
index 5b1339d..d4eef87 100644 (file)
@@ -1,7 +1,30 @@
 <?php
+/**
+ * @defgroup FileRepo File Repository
+ *
+ * @brief This module handles how MediaWiki interacts with filesystems.
+ *
+ * @details
+ */
+
 /**
  * Base code for file repositories.
  *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
  * @file
  * @ingroup FileRepo
  */
  * @ingroup FileRepo
  */
 class FileRepo {
-       const FILES_ONLY = 1;
-
        const DELETE_SOURCE = 1;
        const OVERWRITE = 2;
        const OVERWRITE_SAME = 4;
        const SKIP_LOCKING = 8;
-       const ALLOW_STALE = 16;
 
-       /** @var FileBackendBase */
+       /** @var FileBackend */
        protected $backend;
        /** @var Array Map of zones to config */
        protected $zones = array();
@@ -40,10 +60,23 @@ class FileRepo {
        var $oldFileFactory = false;
        var $fileFactoryKey = false, $oldFileFactoryKey = false;
 
-       function __construct( $info ) {
+       /**
+        * @param $info array|null
+        * @throws MWException
+        */
+       public function __construct( array $info = null ) {
+               // Verify required settings presence
+               if(
+                       $info === null
+                       || !array_key_exists( 'name', $info )
+                       || !array_key_exists( 'backend', $info )
+               ) {
+                       throw new MWException( __CLASS__ . " requires an array of options having both 'name' and 'backend' keys.\n" );
+               }
+
                // Required settings
                $this->name = $info['name'];
-               if ( $info['backend'] instanceof FileBackendBase ) {
+               if ( $info['backend'] instanceof FileBackend ) {
                        $this->backend = $info['backend']; // useful for testing
                } else {
                        $this->backend = FileBackendGroup::singleton()->get( $info['backend'] );
@@ -85,29 +118,39 @@ class FileRepo {
                        : array();
                // Give defaults for the basic zones...
                foreach ( array( 'public', 'thumb', 'temp', 'deleted' ) as $zone ) {
-                       if ( !isset( $this->zones[$zone] ) ) {
-                               $this->zones[$zone] = array(
-                                       'container' => "media-$zone",
-                                       'directory' => '' // container root
-                               );
+                       if ( !isset( $this->zones[$zone]['container'] ) ) {
+                               $this->zones[$zone]['container'] = "{$this->name}-{$zone}";
+                       }
+                       if ( !isset( $this->zones[$zone]['directory'] ) ) {
+                               $this->zones[$zone]['directory'] = '';
                        }
                }
        }
 
        /**
-        * Get the file backend instance
+        * Get the file backend instance. Use this function wisely.
         *
-        * @return FileBackendBase
+        * @return FileBackend
         */
        public function getBackend() {
                return $this->backend;
        }
 
        /**
-        * Prepare a single zone or list of zones for usage.
-        * See initDeletedDir() for additional setup needed for the 'deleted' zone.
-        * 
+        * Get an explanatory message if this repo is read-only.
+        * This checks if an administrator disabled writes to the backend.
+        *
+        * @return string|bool Returns false if the repo is not read-only
+        */
+       public function getReadOnlyReason() {
+               return $this->backend->getReadOnlyReason();
+       }
+
+       /**
+        * Check if a single zone or list of zones is defined for usage
+        *
         * @param $doZones Array Only do a particular zones
+        * @throws MWException
         * @return Status
         */
        protected function initZones( $doZones = array() ) {
@@ -148,7 +191,7 @@ class FileRepo {
         * The suffix, if supplied, is considered to be unencoded, and will be
         * URL-encoded before being returned.
         *
-        * @param $suffix string
+        * @param $suffix string|bool
         * @return string
         */
        public function getVirtualUrl( $suffix = false ) {
@@ -166,6 +209,11 @@ class FileRepo {
         * @return String or false
         */
        public function getZoneUrl( $zone ) {
+               if ( isset( $this->zones[$zone]['url'] )
+                       && in_array( $zone, array( 'public', 'temp', 'thumb' ) ) )
+               {
+                       return $this->zones[$zone]['url']; // custom URL
+               }
                switch ( $zone ) {
                        case 'public':
                                return $this->url;
@@ -181,12 +229,14 @@ class FileRepo {
        }
 
        /**
-        * Get the backend storage path corresponding to a virtual URL
+        * Get the backend storage path corresponding to a virtual URL.
+        * Use this function wisely.
         *
         * @param $url string
+        * @throws MWException
         * @return string
         */
-       function resolveVirtualUrl( $url ) {
+       public function resolveVirtualUrl( $url ) {
                if ( substr( $url, 0, 9 ) != 'mwrepo://' ) {
                        throw new MWException( __METHOD__.': unknown protocol' );
                }
@@ -207,7 +257,7 @@ class FileRepo {
 
        /**
         * The the storage container and base path of a zone
-        * 
+        *
         * @param $zone string
         * @return Array (container, base path) or (null, null)
         */
@@ -222,7 +272,7 @@ class FileRepo {
         * Get the storage path corresponding to one of the zones
         *
         * @param $zone string
-        * @return string|null
+        * @return string|null Returns null if the zone is not defined
         */
        public function getZonePath( $zone ) {
                list( $container, $base ) = $this->getZoneLocation( $zone );
@@ -270,16 +320,16 @@ class FileRepo {
         *
         * @param $title Mixed: Title object or string
         * @param $options array Associative array of options:
-        *     time:           requested time for an archived image, or false for the
+        *     time:           requested time for a specific file version, or false for the
         *                     current version. An image object will be returned which was
-        *                     created at the specified time.
+        *                     created at the specified time (which may be archived or current).
         *
         *     ignoreRedirect: If true, do not follow file redirects
         *
         *     private:        If true, return restricted (deleted) files if the current
         *                     user is allowed to view them. Otherwise, such files will not
         *                     be found.
-        * @return File|false
+        * @return File|bool False on failure
         */
        public function findFile( $title, $options = array() ) {
                $title = File::normalizeTitle( $title );
@@ -328,7 +378,7 @@ class FileRepo {
        /**
         * Find many files at once.
         *
-        * @param $items An array of titles, or an array of findFile() options with
+        * @param $items array An array of titles, or an array of findFile() options with
         *    the "title" option giving the title. Example:
         *
         *     $findItem = array( 'title' => $title, 'private' => true );
@@ -336,7 +386,7 @@ class FileRepo {
         *     $repo->findFiles( $findBatch );
         * @return array
         */
-       public function findFiles( $items ) {
+       public function findFiles( array $items ) {
                $result = array();
                foreach ( $items as $item ) {
                        if ( is_array( $item ) ) {
@@ -361,12 +411,11 @@ class FileRepo {
         * version control should return false if the time is specified.
         *
         * @param $sha1 String base 36 SHA-1 hash
-        * @param $options Option array, same as findFile().
-        * @return File|false
+        * @param $options array Option array, same as findFile().
+        * @return File|bool False on failure
         */
        public function findFileFromKey( $sha1, $options = array() ) {
                $time = isset( $options['time'] ) ? $options['time'] : false;
-
                # First try to find a matching current version of a file...
                if ( $this->fileFactoryKey ) {
                        $img = call_user_func( $this->fileFactoryKey, $sha1, $this, $time );
@@ -395,6 +444,8 @@ class FileRepo {
         * SHA-1 content hash.
         *
         * STUB
+        * @param $hash
+        * @return array
         */
        public function findBySha1( $hash ) {
                return array();
@@ -403,19 +454,11 @@ class FileRepo {
        /**
         * Get the public root URL of the repository
         *
-        * @return string|false
-        */
-       public function getRootUrl() {
-               return $this->url;
-       }
-
-       /**
-        * Returns true if the repository uses a multi-level directory structure
-        *
+        * @deprecated since 1.20
         * @return string
         */
-       public function isHashed() {
-               return (bool)$this->hashLevels;
+       public function getRootUrl() {
+               return $this->getZoneUrl( 'public' );
        }
 
        /**
@@ -440,6 +483,7 @@ class FileRepo {
         * Get the name of an image from its title object
         *
         * @param $title Title
+        * @return String
         */
        public function getNameFromTitle( Title $title ) {
                global $wgContLang;
@@ -467,19 +511,32 @@ class FileRepo {
         * Get a relative path including trailing slash, e.g. f/fa/
         * If the repo is not hashed, returns an empty string
         *
-        * @param $name string
+        * @param $name string Name of file
         * @return string
         */
        public function getHashPath( $name ) {
                return self::getHashPathForLevel( $name, $this->hashLevels );
        }
 
+       /**
+        * Get a relative path including trailing slash, e.g. f/fa/
+        * If the repo is not hashed, returns an empty string
+        *
+        * @param $suffix string Basename of file from FileRepo::storeTemp()
+        * @return string
+        */
+       public function getTempHashPath( $suffix ) {
+               $parts = explode( '!', $suffix, 2 ); // format is <timestamp>!<name> or just <name>
+               $name = isset( $parts[1] ) ? $parts[1] : $suffix; // hash path is not based on timestamp
+               return self::getHashPathForLevel( $name, $this->hashLevels );
+       }
+
        /**
         * @param $name
         * @param $levels
         * @return string
         */
-       static function getHashPathForLevel( $name, $levels ) {
+       protected static function getHashPathForLevel( $name, $levels ) {
                if ( $levels == 0 ) {
                        return '';
                } else {
@@ -515,7 +572,7 @@ class FileRepo {
         *
         * @param $query mixed Query string to append
         * @param $entry string Entry point; defaults to index
-        * @return string|false
+        * @return string|bool False on failure
         */
        public function makeUrl( $query = '', $entry = 'index' ) {
                if ( isset( $this->scriptDirUrl ) ) {
@@ -595,7 +652,7 @@ class FileRepo {
        /**
         * Get the URL of the stylesheet to apply to description pages
         *
-        * @return string|false
+        * @return string|bool False on failure
         */
        public function getDescriptionStylesheetUrl() {
                if ( isset( $this->scriptDirUrl ) ) {
@@ -617,14 +674,16 @@ class FileRepo {
         *     self::OVERWRITE_SAME    Overwrite the file if the destination exists and has the
         *                             same contents as the source
         *     self::SKIP_LOCKING      Skip any file locking when doing the store
-        *     self::ALLOW_STALE       Don't require latest data for existence checks
         * @return FileRepoStatus
         */
        public function store( $srcPath, $dstZone, $dstRel, $flags = 0 ) {
+               $this->assertWritableRepo(); // fail out if read-only
+
                $status = $this->storeBatch( array( array( $srcPath, $dstZone, $dstRel ) ), $flags );
                if ( $status->successCount == 0 ) {
                        $status->ok = false;
                }
+
                return $status;
        }
 
@@ -638,18 +697,23 @@ class FileRepo {
         *     self::OVERWRITE_SAME    Overwrite the file if the destination exists and has the
         *                             same contents as the source
         *     self::SKIP_LOCKING      Skip any file locking when doing the store
+        * @throws MWException
         * @return FileRepoStatus
         */
-       public function storeBatch( $triplets, $flags = 0 ) {
-               $backend = $this->backend; // convenience
+       public function storeBatch( array $triplets, $flags = 0 ) {
+               $this->assertWritableRepo(); // fail out if read-only
 
                $status = $this->newGood();
+               $backend = $this->backend; // convenience
 
                $operations = array();
                $sourceFSFilesToDelete = array(); // cleanup for disk source files
                // Validate each triplet and get the store operation...
                foreach ( $triplets as $triplet ) {
                        list( $srcPath, $dstZone, $dstRel ) = $triplet;
+                       wfDebug( __METHOD__
+                               . "( \$src='$srcPath', \$dstZone='$dstZone', \$dstRel='$dstRel' )\n"
+                       );
 
                        // Resolve destination path
                        $root = $this->getZonePath( $dstZone );
@@ -660,8 +724,7 @@ class FileRepo {
                                throw new MWException( 'Validation error in $dstRel' );
                        }
                        $dstPath = "$root/$dstRel";
-                       $dstDir = dirname( $dstPath );
-
+                       $dstDir  = dirname( $dstPath );
                        // Create destination directories for this triplet
                        if ( !$backend->prepare( array( 'dir' => $dstDir ) )->isOK() ) {
                                return $this->newFatal( 'directorycreateerror', $dstDir );
@@ -672,9 +735,7 @@ class FileRepo {
                        }
 
                        // Resolve source to a storage path if virtual
-                       if ( self::isVirtualUrl( $srcPath ) ) {
-                               $srcPath = $this->resolveVirtualUrl( $srcPath );
-                       }
+                       $srcPath = $this->resolveToStoragePath( $srcPath );
 
                        // Get the appropriate file operation
                        if ( FileBackend::isStoragePath( $srcPath ) ) {
@@ -689,19 +750,16 @@ class FileRepo {
                                'op'            => $opName,
                                'src'           => $srcPath,
                                'dst'           => $dstPath,
-                               'overwriteDest' => $flags & self::OVERWRITE,
+                               'overwrite'     => $flags & self::OVERWRITE,
                                'overwriteSame' => $flags & self::OVERWRITE_SAME,
                        );
                }
 
                // Execute the store operation for each triplet
-               $opts = array( 'ignoreErrors' => true );
+               $opts = array( 'force' => true );
                if ( $flags & self::SKIP_LOCKING ) {
                        $opts['nonLocking'] = true;
                }
-               if ( $flags & self::ALLOW_STALE ) {
-                       $opts['allowStale'] = true;
-               }
                $status->merge( $backend->doOperations( $operations, $opts ) );
                // Cleanup for disk source files...
                foreach ( $sourceFSFilesToDelete as $file ) {
@@ -715,54 +773,135 @@ class FileRepo {
 
        /**
         * Deletes a batch of files.
-        * Each file can be a (zone, rel) pair, virtual url, storage path, or FS path.
+        * Each file can be a (zone, rel) pair, virtual url, storage path.
         * It will try to delete each file, but ignores any errors that may occur.
         *
-        * @param $pairs array List of files to delete
-        * @return void
+        * @param $files array List of files to delete
+        * @param $flags Integer: bitwise combination of the following flags:
+        *     self::SKIP_LOCKING      Skip any file locking when doing the deletions
+        * @return FileRepoStatus
         */
-       public function cleanupBatch( $files ) {
+       public function cleanupBatch( array $files, $flags = 0 ) {
+               $this->assertWritableRepo(); // fail out if read-only
+
+               $status = $this->newGood();
+
                $operations = array();
-               $sourceFSFilesToDelete = array(); // cleanup for disk source files
-               foreach ( $files as $file ) {
-                       if ( is_array( $file ) ) {
+               foreach ( $files as $path ) {
+                       if ( is_array( $path ) ) {
                                // This is a pair, extract it
-                               list( $zone, $rel ) = $file;
-                               $root = $this->getZonePath( $zone );
-                               $path = "$root/$rel";
-                       } else {
-                               if ( self::isVirtualUrl( $file ) ) {
-                                       // This is a virtual url, resolve it
-                                       $path = $this->resolveVirtualUrl( $file );
-                               } else {
-                                       // This is a full file name
-                                       $path = $file;
-                               }
-                       }
-                       // Get a file operation if needed
-                       if ( FileBackend::isStoragePath( $path ) ) {
-                               $operations[] = array(
-                                       'op'           => 'delete',
-                                       'src'          => $path,
-                               );
+                               list( $zone, $rel ) = $path;
+                               $path = $this->getZonePath( $zone ) . "/$rel";
                        } else {
-                               $sourceFSFilesToDelete[] = $path;
+                               // Resolve source to a storage path if virtual
+                               $path = $this->resolveToStoragePath( $path );
                        }
+                       $operations[] = array( 'op' => 'delete', 'src' => $path );
                }
                // Actually delete files from storage...
-               $opts = array( 'ignoreErrors' => true );
-               $this->backend->doOperations( $operations, $opts );
-               // Cleanup for disk source files...
-               foreach ( $sourceFSFilesToDelete as $file ) {
-                       wfSuppressWarnings();
-                       unlink( $file ); // FS cleanup
-                       wfRestoreWarnings();
+               $opts = array( 'force' => true );
+               if ( $flags & self::SKIP_LOCKING ) {
+                       $opts['nonLocking'] = true;
                }
+               $status->merge( $this->backend->doOperations( $operations, $opts ) );
+
+               return $status;
+       }
+
+       /**
+        * Import a file from the local file system into the repo.
+        * This does no locking nor journaling and overrides existing files.
+        * This function can be used to write to otherwise read-only foreign repos.
+        * This is intended for copying generated thumbnails into the repo.
+        *
+        * @param $src string File system path
+        * @param $dst string Virtual URL or storage path
+        * @return FileRepoStatus
+        */
+       final public function quickImport( $src, $dst ) {
+               return $this->quickImportBatch( array( array( $src, $dst ) ) );
+       }
+
+       /**
+        * Purge a file from the repo. This does no locking nor journaling.
+        * This function can be used to write to otherwise read-only foreign repos.
+        * This is intended for purging thumbnails.
+        *
+        * @param $path string Virtual URL or storage path
+        * @return FileRepoStatus
+        */
+       final public function quickPurge( $path ) {
+               return $this->quickPurgeBatch( array( $path ) );
+       }
+
+       /**
+        * Deletes a directory if empty.
+        * This function can be used to write to otherwise read-only foreign repos.
+        *
+        * @param $dir string Virtual URL (or storage path) of directory to clean
+        * @return Status
+        */
+       public function quickCleanDir( $dir ) {
+               $status = $this->newGood();
+               $status->merge( $this->backend->clean(
+                       array( 'dir' => $this->resolveToStoragePath( $dir ) ) ) );
+
+               return $status;
+       }
+
+       /**
+        * Import a batch of files from the local file system into the repo.
+        * This does no locking nor journaling and overrides existing files.
+        * This function can be used to write to otherwise read-only foreign repos.
+        * This is intended for copying generated thumbnails into the repo.
+        *
+        * @param $pairs Array List of tuples (file system path, virtual URL or storage path)
+        * @return FileRepoStatus
+        */
+       public function quickImportBatch( array $pairs ) {
+               $status = $this->newGood();
+               $operations = array();
+               foreach ( $pairs as $pair ) {
+                       list ( $src, $dst ) = $pair;
+                       $operations[] = array(
+                               'op'        => 'store',
+                               'src'       => $src,
+                               'dst'       => $this->resolveToStoragePath( $dst )
+                       );
+                       $this->backend->prepare( array( 'dir' => dirname( $dst ) ) );
+               }
+               $status->merge( $this->backend->doQuickOperations( $operations ) );
+
+               return $status;
+       }
+
+       /**
+        * Purge a batch of files from the repo.
+        * This function can be used to write to otherwise read-only foreign repos.
+        * This does no locking nor journaling and is intended for purging thumbnails.
+        *
+        * @param $paths Array List of virtual URLs or storage paths
+        * @return FileRepoStatus
+        */
+       public function quickPurgeBatch( array $paths ) {
+               $status = $this->newGood();
+               $operations = array();
+               foreach ( $paths as $path ) {
+                       $operations[] = array(
+                               'op'                  => 'delete',
+                               'src'                 => $this->resolveToStoragePath( $path ),
+                               'ignoreMissingSource' => true
+                       );
+               }
+               $status->merge( $this->backend->doQuickOperations( $operations ) );
+
+               return $status;
        }
 
        /**
         * Pick a random name in the temp zone and store a file to it.
-        * Returns a FileRepoStatus object with the URL in the value.
+        * Returns a FileRepoStatus object with the file Virtual URL in the value,
+        * file can later be disposed using FileRepo::freeTemp().
         *
         * @param $originalName String: the base name of the file as specified
         *     by the user. The file extension will be maintained.
@@ -770,29 +909,32 @@ class FileRepo {
         * @return FileRepoStatus object with the URL in the value.
         */
        public function storeTemp( $originalName, $srcPath ) {
-               $date = gmdate( "YmdHis" );
-               $hashPath = $this->getHashPath( $originalName );
-               $dstRel = "{$hashPath}{$date}!{$originalName}";
+               $this->assertWritableRepo(); // fail out if read-only
+
+               $date      = gmdate( "YmdHis" );
+               $hashPath  = $this->getHashPath( $originalName );
+               $dstRel    = "{$hashPath}{$date}!{$originalName}";
                $dstUrlRel = $hashPath . $date . '!' . rawurlencode( $originalName );
 
                $result = $this->store( $srcPath, 'temp', $dstRel, self::SKIP_LOCKING );
                $result->value = $this->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
+
                return $result;
        }
 
        /**
-        * Concatenate a list of files into a target file location. 
-        * 
+        * Concatenate a list of files into a target file location.
+        *
         * @param $srcPaths Array Ordered list of source virtual URLs/storage paths
         * @param $dstPath String Target file system path
         * @param $flags Integer: bitwise combination of the following flags:
         *     self::DELETE_SOURCE     Delete the source files
         * @return FileRepoStatus
         */
-       function concatenate( $srcPaths, $dstPath, $flags = 0 ) {
+       public function concatenate( array $srcPaths, $dstPath, $flags = 0 ) {
+               $this->assertWritableRepo(); // fail out if read-only
+
                $status = $this->newGood();
-               // Resolve target to a storage path if virtual
-               $dest = $this->resolveToStoragePath( $dstPath );
 
                $sources = array();
                $deleteOperations = array(); // post-concatenate ops
@@ -805,16 +947,16 @@ class FileRepo {
                        }
                }
 
-               // Concatenate the chunks into one file
-               $op = array( 'op' => 'concatenate', 'srcs' => $sources, 'dst' => $dest );
-               $status->merge( $this->backend->doOperation( $op ) );
+               // Concatenate the chunks into one FS file
+               $params = array( 'srcs' => $sources, 'dst' => $dstPath );
+               $status->merge( $this->backend->concatenate( $params ) );
                if ( !$status->isOK() ) {
                        return $status;
                }
 
                // Delete the sources if required
                if ( $deleteOperations ) {
-                       $opts = array( 'ignoreErrors' => true );
+                       $opts = array( 'force' => true );
                        $status->merge( $this->backend->doOperations( $deleteOperations, $opts ) );
                }
 
@@ -827,19 +969,20 @@ class FileRepo {
        /**
         * Remove a temporary file or mark it for garbage collection
         *
-        * @param $virtualUrl String: the virtual URL returned by storeTemp
+        * @param $virtualUrl String: the virtual URL returned by FileRepo::storeTemp()
         * @return Boolean: true on success, false on failure
         */
        public function freeTemp( $virtualUrl ) {
+               $this->assertWritableRepo(); // fail out if read-only
+
                $temp = "mwrepo://{$this->name}/temp";
                if ( substr( $virtualUrl, 0, strlen( $temp ) ) != $temp ) {
                        wfDebug( __METHOD__.": Invalid temp virtual URL\n" );
                        return false;
                }
                $path = $this->resolveVirtualUrl( $virtualUrl );
-               $op = array( 'op' => 'delete', 'src' => $path );
-               $status = $this->backend->doOperation( $op );
-               return $status->isOK();
+
+               return $this->cleanupBatch( array( $path ), self::SKIP_LOCKING )->isOK();
        }
 
        /**
@@ -855,8 +998,11 @@ class FileRepo {
         *        be archived, if there is one. Relative to the public zone root.
         * @param $flags Integer: bitfield, may be FileRepo::DELETE_SOURCE to indicate
         *        that the source file should be deleted if possible
+        * @return FileRepoStatus
         */
        public function publish( $srcPath, $dstRel, $archiveRel, $flags = 0 ) {
+               $this->assertWritableRepo(); // fail out if read-only
+
                $status = $this->publishBatch( array( array( $srcPath, $dstRel, $archiveRel ) ), $flags );
                if ( $status->successCount == 0 ) {
                        $status->ok = false;
@@ -866,6 +1012,7 @@ class FileRepo {
                } else {
                        $status->value = false;
                }
+
                return $status;
        }
 
@@ -875,11 +1022,13 @@ class FileRepo {
         * @param $triplets Array: (source, dest, archive) triplets as per publish()
         * @param $flags Integer: bitfield, may be FileRepo::DELETE_SOURCE to indicate
         *        that the source files should be deleted if possible
+        * @throws MWException
         * @return FileRepoStatus
         */
-       public function publishBatch( $triplets, $flags = 0 ) {
-               $backend = $this->backend; // convenience
+       public function publishBatch( array $triplets, $flags = 0 ) {
+               $this->assertWritableRepo(); // fail out if read-only
 
+               $backend = $this->backend; // convenience
                // Try creating directories
                $status = $this->initZones( 'public' );
                if ( !$status->isOK() ) {
@@ -894,9 +1043,7 @@ class FileRepo {
                foreach ( $triplets as $i => $triplet ) {
                        list( $srcPath, $dstRel, $archiveRel ) = $triplet;
                        // Resolve source to a storage path if virtual
-                       if ( substr( $srcPath, 0, 9 ) == 'mwrepo://' ) {
-                               $srcPath = $this->resolveVirtualUrl( $srcPath );
-                       }
+                       $srcPath = $this->resolveToStoragePath( $srcPath );
                        if ( !$this->validateFilename( $dstRel ) ) {
                                throw new MWException( 'Validation error in $dstRel' );
                        }
@@ -967,7 +1114,7 @@ class FileRepo {
                }
 
                // Execute the operations for each triplet
-               $opts = array( 'ignoreErrors' => true );
+               $opts = array( 'force' => true );
                $status->merge( $backend->doOperations( $operations, $opts ) );
                // Cleanup for disk source files...
                foreach ( $sourceFSFilesToDelete as $file ) {
@@ -979,16 +1126,30 @@ class FileRepo {
                return $status;
        }
 
+       /**
+        * Deletes a directory if empty.
+        *
+        * @param $dir string Virtual URL (or storage path) of directory to clean
+        * @return Status
+        */
+       public function cleanDir( $dir ) {
+               $this->assertWritableRepo(); // fail out if read-only
+
+               $status = $this->newGood();
+               $status->merge( $this->backend->clean(
+                       array( 'dir' => $this->resolveToStoragePath( $dir ) ) ) );
+
+               return $status;
+       }
+
        /**
         * Checks existence of a a file
         *
-        * @param $file Virtual URL (or storage path) of file to check
-        * @param $flags Integer: bitwise combination of the following flags:
-        *     self::FILES_ONLY     Mark file as existing only if it is a file (not directory)
+        * @param $file string Virtual URL (or storage path) of file to check
         * @return bool
         */
-       public function fileExists( $file, $flags = 0 ) {
-               $result = $this->fileExistsBatch( array( $file ), $flags );
+       public function fileExists( $file ) {
+               $result = $this->fileExistsBatch( array( $file ) );
                return $result[0];
        }
 
@@ -996,27 +1157,14 @@ class FileRepo {
         * Checks existence of an array of files.
         *
         * @param $files Array: Virtual URLs (or storage paths) of files to check
-        * @param $flags Integer: bitwise combination of the following flags:
-        *     self::FILES_ONLY     Mark file as existing only if it is a file (not directory)
-        * @return Either array of files and existence flags, or false
+        * @return array|bool Either array of files and existence flags, or false
         */
-       public function fileExistsBatch( $files, $flags = 0 ) {
+       public function fileExistsBatch( array $files ) {
                $result = array();
                foreach ( $files as $key => $file ) {
-                       if ( self::isVirtualUrl( $file ) ) {
-                               $file = $this->resolveVirtualUrl( $file );
-                       }
-                       if ( FileBackend::isStoragePath( $file ) ) {
-                               $result[$key] = $this->backend->fileExists( array( 'src' => $file ) );
-                       } else {
-                               if ( $flags & self::FILES_ONLY ) {
-                                       $result[$key] = is_file( $file ); // FS only
-                               } else {
-                                       $result[$key] = file_exists( $file ); // FS only
-                               }
-                       }
+                       $file = $this->resolveToStoragePath( $file );
+                       $result[$key] = $this->backend->fileExists( array( 'src' => $file ) );
                }
-
                return $result;
        }
 
@@ -1031,6 +1179,8 @@ class FileRepo {
         * @return FileRepoStatus object
         */
        public function delete( $srcRel, $archiveRel ) {
+               $this->assertWritableRepo(); // fail out if read-only
+
                return $this->deleteBatch( array( array( $srcRel, $archiveRel ) ) );
        }
 
@@ -1048,10 +1198,11 @@ class FileRepo {
         *        is a two-element array containing the source file path relative to the
         *        public root in the first element, and the archive file path relative
         *        to the deleted zone root in the second element.
+        * @throws MWException
         * @return FileRepoStatus
         */
-       public function deleteBatch( $sourceDestPairs ) {
-               $backend = $this->backend; // convenience
+       public function deleteBatch( array $sourceDestPairs ) {
+               $this->assertWritableRepo(); // fail out if read-only
 
                // Try creating directories
                $status = $this->initZones( array( 'public', 'deleted' ) );
@@ -1061,14 +1212,14 @@ class FileRepo {
 
                $status = $this->newGood();
 
+               $backend = $this->backend; // convenience
                $operations = array();
                // Validate filenames and create archive directories
                foreach ( $sourceDestPairs as $pair ) {
                        list( $srcRel, $archiveRel ) = $pair;
                        if ( !$this->validateFilename( $srcRel ) ) {
                                throw new MWException( __METHOD__.':Validation error in $srcRel' );
-                       }
-                       if ( !$this->validateFilename( $archiveRel ) ) {
+                       } elseif ( !$this->validateFilename( $archiveRel ) ) {
                                throw new MWException( __METHOD__.':Validation error in $archiveRel' );
                        }
 
@@ -1085,33 +1236,39 @@ class FileRepo {
                        }
                        $this->initDeletedDir( $archiveDir );
 
-                       if ( $backend->fileExists( array( 'src' => $archivePath ) ) ) {
-                               $operations[] = array(
-                                       'op'  => 'delete',
-                                       'src' => $srcPath
-                               );
-                       } else {
-                               $operations[] = array(
-                                       'op'  => 'move',
-                                       'src' => $srcPath,
-                                       'dst' => $archivePath
-                               );
-                       }
+                       $operations[] = array(
+                               'op'            => 'move',
+                               'src'           => $srcPath,
+                               'dst'           => $archivePath,
+                               // We may have 2+ identical files being deleted,
+                               // all of which will map to the same destination file
+                               'overwriteSame' => true // also see bug 31792
+                       );
                }
 
                // Move the files by execute the operations for each pair.
                // We're now committed to returning an OK result, which will
                // lead to the files being moved in the DB also.
-               $opts = array( 'ignoreErrors' => true );
+               $opts = array( 'force' => true );
                $status->merge( $backend->doOperations( $operations, $opts ) );
 
                return $status;
        }
 
+       /**
+        * Delete files in the deleted directory if they are not referenced in the filearchive table
+        *
+        * STUB
+        */
+       public function cleanupDeletedBatch( array $storageKeys ) {
+               $this->assertWritableRepo();
+       }
+
        /**
         * Get a relative path for a deletion archive key,
         * e.g. s/z/a/ for sza251lrxrc1jad41h5mgilp8nysje52.jpg
         *
+        * @param $key string
         * @return string
         */
        public function getDeletedHashPath( $key ) {
@@ -1140,7 +1297,7 @@ class FileRepo {
        /**
         * Get a local FS copy of a file with a given virtual URL/storage path.
         * Temporary files may be purged when the file object falls out of scope.
-        * 
+        *
         * @param $virtualUrl string
         * @return TempFSFile|null Returns null on failure
         */
@@ -1153,7 +1310,7 @@ class FileRepo {
         * Get a local FS file with a given virtual URL/storage path.
         * The file is either an original or a copy. It should not be changed.
         * Temporary files may be purged when the file object falls out of scope.
-        * 
+        *
         * @param $virtualUrl string
         * @return FSFile|null Returns null on failure.
         */
@@ -1178,7 +1335,7 @@ class FileRepo {
         * Get the timestamp of a file with a given virtual URL/storage path
         *
         * @param $virtualUrl string
-        * @return string|false
+        * @return string|bool False on failure
         */
        public function getFileTimestamp( $virtualUrl ) {
                $path = $this->resolveToStoragePath( $virtualUrl );
@@ -1189,7 +1346,7 @@ class FileRepo {
         * Get the sha1 of a file with a given virtual URL/storage path
         *
         * @param $virtualUrl string
-        * @return string|false
+        * @return string|bool
         */
        public function getFileSha1( $virtualUrl ) {
                $path = $this->resolveToStoragePath( $virtualUrl );
@@ -1214,7 +1371,8 @@ class FileRepo {
        }
 
        /**
-        * Call a callback function for every public file in the repository.
+        * Call a callback function for every public regular file in the repository.
+        * This only acts on the current version of files, not any old versions.
         * May use either the database or the filesystem.
         *
         * @param $callback Array|string
@@ -1260,23 +1418,7 @@ class FileRepo {
                if ( strval( $filename ) == '' ) {
                        return false;
                }
-               if ( wfIsWindows() ) {
-                       $filename = strtr( $filename, '\\', '/' );
-               }
-               /**
-                * Use the same traversal protection as Title::secureAndSplit()
-                */
-               if ( strpos( $filename, '.' ) !== false &&
-                       ( $filename === '.' || $filename === '..' ||
-                               strpos( $filename, './' ) === 0  ||
-                               strpos( $filename, '../' ) === 0 ||
-                               strpos( $filename, '/./' ) !== false ||
-                               strpos( $filename, '/../' ) !== false ) )
-               {
-                       return false;
-               } else {
-                       return true;
-               }
+               return FileBackend::isPathTraversalFree( $filename );
        }
 
        /**
@@ -1287,11 +1429,9 @@ class FileRepo {
        function getErrorCleanupFunction() {
                switch ( $this->pathDisclosureProtection ) {
                        case 'none':
+                       case 'simple': // b/c
                                $callback = array( $this, 'passThrough' );
                                break;
-                       case 'simple':
-                               $callback = array( $this, 'simpleClean' );
-                               break;
                        default: // 'paranoid'
                                $callback = array( $this, 'paranoidClean' );
                }
@@ -1308,22 +1448,6 @@ class FileRepo {
                return '[hidden]';
        }
 
-       /**
-        * Path disclosure protection function
-        *
-        * @param $param string
-        * @return string
-        */
-       function simpleClean( $param ) {
-               global $IP;
-               if ( !isset( $this->simpleCleanPairs ) ) {
-                       $this->simpleCleanPairs = array(
-                               $IP => '$IP', // sanity
-                       );
-               }
-               return strtr( $param, $this->simpleCleanPairs );
-       }
-
        /**
         * Path disclosure protection function
         *
@@ -1339,7 +1463,7 @@ class FileRepo {
         *
         * @return FileRepoStatus
         */
-       function newFatal( $message /*, parameters...*/ ) {
+       public function newFatal( $message /*, parameters...*/ ) {
                $params = func_get_args();
                array_unshift( $params, $this );
                return MWInit::callStaticMethod( 'FileRepoStatus', 'newFatal', $params );
@@ -1348,19 +1472,13 @@ class FileRepo {
        /**
         * Create a new good result
         *
+        * @param $value null|string
         * @return FileRepoStatus
         */
-       function newGood( $value = null ) {
+       public function newGood( $value = null ) {
                return FileRepoStatus::newGood( $this, $value );
        }
 
-       /**
-        * Delete files in the deleted directory if they are not referenced in the filearchive table
-        *
-        * STUB
-        */
-       public function cleanupDeletedBatch( $storageKeys ) {}
-
        /**
         * Checks if there is a redirect named as $title. If there is, return the
         * title object. If not, return false.
@@ -1411,8 +1529,9 @@ class FileRepo {
         * The parameters are the parts of the key, as for wfMemcKey().
         *
         * STUB
+        * @return bool
         */
-       function getSharedCacheKey( /*...*/ ) {
+       public function getSharedCacheKey( /*...*/ ) {
                return false;
        }
 
@@ -1423,12 +1542,42 @@ class FileRepo {
         *
         * @return string
         */
-       function getLocalCacheKey( /*...*/ ) {
+       public function getLocalCacheKey( /*...*/ ) {
                $args = func_get_args();
                array_unshift( $args, 'filerepo', $this->getName() );
                return call_user_func_array( 'wfMemcKey', $args );
        }
 
+       /**
+        * Get an temporary FileRepo associated with this repo.
+        * Files will be created in the temp zone of this repo and
+        * thumbnails in a /temp subdirectory in thumb zone of this repo.
+        * It will have the same backend as this repo.
+        *
+        * @return TempFileRepo
+        */
+       public function getTempRepo() {
+               return new TempFileRepo( array(
+                       'name'      => "{$this->name}-temp",
+                       'backend'   => $this->backend,
+                       'zones'     => array(
+                               'public' => array(
+                                       'container' => $this->zones['temp']['container'],
+                                       'directory' => $this->zones['temp']['directory']
+                               ),
+                               'thumb'  => array(
+                                       'container' => $this->zones['thumb']['container'],
+                                       'directory' => ( $this->zones['thumb']['directory'] == '' )
+                                               ? 'temp'
+                                               : $this->zones['thumb']['directory'] . '/temp'
+                               )
+                       ),
+                       'url'        => $this->getZoneUrl( 'temp' ),
+                       'thumbUrl'   => $this->getZoneUrl( 'thumb' ) . '/temp',
+                       'hashLevels' => $this->hashLevels // performance
+               ) );
+       }
+
        /**
         * Get an UploadStash associated with this repo.
         *
@@ -1437,4 +1586,22 @@ class FileRepo {
        public function getUploadStash() {
                return new UploadStash( $this );
        }
+
+       /**
+        * Throw an exception if this repo is read-only by design.
+        * This does not and should not check getReadOnlyReason().
+        *
+        * @return void
+        * @throws MWException
+        */
+       protected function assertWritableRepo() {}
+}
+
+/**
+ * FileRepo for temporary files created via FileRepo::getTempRepo()
+ */
+class TempFileRepo extends FileRepo {
+       public function getTempRepo() {
+               throw new MWException( "Cannot get a temp repo from a temp repo." );
+       }
 }